package oa.hleast.xswl.utils;

import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 */
@SuppressWarnings("all")
@Component
@Slf4j
public class GlobalExpressUtil {

    public static void main(String[] args) {
        findDelivery("SF","SF1314492117492", "17788149965");
    }

    public static String findDelivery(String company, String num, String phone) {

        // 快递公司简称 https://www.kaifain.com/s/6d23ad5fcd9a
        String path = "http://gwgp-jbvaqrrwly2.n.bdcloudapi.com/gxali";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.GET, path);

        request.setCredentials("16b09608d5684a27b6e47a1477e089a3", "714d4e67174449eea477bab002ea54ca");

        request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");


        String strNum = num;
        if (strNum.startsWith("SF")) strNum += ": " + phone.substring(phone.length() - 4);
        System.out.println(strNum);
        request.addQueryParameter("n", strNum);
//        request.addQueryParameter("t", company);

        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());
        String result = "";
        try {
            ApiExplorerResponse response = client.sendRequest(request);
            // 返回结果格式为Json字符串
            result = response.getResult();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}

